package com.lvy.hczn.transmit.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.GoodsBoxSpec;
import com.lvy.hczn.front.system.service.GoodsBoxSpecService;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.transmit.domain.dto.cteck.ContainerInfo;
import com.lvy.hczn.transmit.service.TGoodsBoxSpecService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class TGoodsBoxSpecServiceImpl implements TGoodsBoxSpecService {

    @Autowired
    private GoodsBoxSpecService goodsBoxSpecService;

    /**
     * 从平台同步规格
     * 数据字段转换
     *
     * @param info
     */
    @Override
    public void syncBoxSpecFromPlatForm(ParamInfo<ContainerInfo> info) {

        if (info != null && info.getData() != null) {
            ContainerInfo containerInfo = JSON.parseObject(JSON.toJSONString(info.getData()), ContainerInfo.class);
            log.error("syncBoxSpecFromPlatForm:{}", JSONUtil.toJsonStr(info));
            if (StrUtil.hasEmpty(containerInfo.getContainerName(), containerInfo.getId())) {
                throw new UtilException("编码或名称为空");
            }
            if (containerInfo.getLength() == null || containerInfo.getWidth() == null || containerInfo.getHeight() == null || containerInfo.getVolume() == null) {
                throw new UtilException("长宽高或体积为空");
            }
            if (containerInfo.getContainerOccupancy() == null) {
                throw new UtilException("货箱占位为空");
            }
            if (containerInfo.getIsDelete() == null) {
                throw new UtilException("是否删除状态为空");
            }
            GoodsBoxSpec spec = new GoodsBoxSpec();
            spec.setId(containerInfo.getId());
            spec.setSpecCode(containerInfo.getId());
            spec.setSpecName(containerInfo.getContainerName());
            spec.setLength(containerInfo.getLength());
            spec.setWidth(containerInfo.getWidth());
            spec.setHeight(containerInfo.getHeight());
            spec.setVolume(containerInfo.getVolume());
            spec.setDigitCapacity(containerInfo.getContainerOccupancy());
            spec.setIsDelete(containerInfo.getIsDelete());
            ParamInfo<GoodsBoxSpec> paramInfo = new ParamInfo<>();
            paramInfo.setData(spec);
            goodsBoxSpecService.syncToTransmitFromPlatform(paramInfo);
        } else {
            log.error("syncBoxSpecFromPlatFormError:{}", JSON.toJSONString(info));
            throw new UtilException("无参数信息");
        }
    }

    /**
     * 货箱规格列表
     *
     * @param info
     * @return void
     * @author yw
     * @date 2023-05-25 14:13:18
     */
    @Override
    public List<GoodsBoxSpec> specList(ParamInfo<GoodsBoxSpec> info) {
        return goodsBoxSpecService.list(Wrappers.<GoodsBoxSpec>lambdaQuery().eq(GoodsBoxSpec::getIsDelete,Constants.IS_DELETE_FALSE));
    }
}
